import scrapy
from scrapy import Request
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

from app import evn_
from spiders.items import StockInfoItem


class StockInfoSingle(scrapy.Spider):
    '''
    行业行情数据（涨幅，资金情况）
    '''
    name = 'stock_info_single'
    start_url = ''

    def start_requests(self):
        '''
        代替start_urls,从数据库读取板块编码，拼接读取板块的行情数据
        :return: 示例：http://quote.eastmoney.com/bk/90.BK0421.html
        '''
        if self.start_url:
            for url in self.start_url:
                yield Request(url, callback=self.parse_item)

    def __init__(self, start_url):
        self.start_url = start_url
        chrome_options = Options()
        chrome_options.add_argument('--disable-gpu')
        chrome_options.add_argument('lang=zh_CN.UTF-8')
        chrome_options.add_argument('headless')  # 无头浏览器
        chrome_options.add_argument('--no-sandbox')  # 必要！！
        chrome_options.add_argument('--disable-dev-shm-usage')  # 必要！！
        prefs = {
            "profile.managed_default_content_settings.images": 2,  # 禁止加载图片
            'permissions.default.stylesheet': 2,  # 禁止加载css
        }
        chrome_options.add_experimental_option("prefs", prefs)
        self.bro = webdriver.Chrome(executable_path=evn_.CHROMEDRIVER_PATH, chrome_options=chrome_options)

    def parse_item(self, response):
        # 涨幅情况
        stock_code = response.url.split('/')[-1][2:8]
        item = StockInfoItem()
        item['industry_name'] = response.xpath('//*[@id="brendnav-item-4"]/text()').extract_first()
        item['stock_code'] = stock_code
        item['stock_name'] = response.xpath('//*[@id="brendnav-item-5"]/text()').extract_first()
        item['market'] = response.url.split('/')[-1][0:2]
        item['stock_detail_url'] = response.url
        yield item
